// System includes
#include <QDebug>
#include <QPlastiqueStyle>
#include <string>

// Local includes
#include "yigsynthmenu.h"
#include "graphics/yigcolorkit.h"

YigSynthMenu::YigSynthMenu(QString appDirectory, QWidget *parent) :
    QWidget(parent),
    scrollView(this),
    groupBox(&groupWidget),
    synthDirectory(appDirectory+"/synthdefs")
{
    if(!synthDirectory.exists())
    {
        qWarning("Cannot find the synthdefs directory");
    }

    QChar arrow(0x2190);
    setWindowTitle(QString(arrow) + "Drag Synths Out");
    setGeometry( QRect( 0, 0, 145, 300 ) );
    setWindowFlags( Qt::Tool | Qt::WindowTitleHint | Qt::CustomizeWindowHint );
    setFixedWidth(145);
    setFixedHeight(300);
    setWindowOpacity(0.77);

    QStyle* yigStyle = new QPlastiqueStyle;
    QPalette yigPalette;
    yigPalette.setColor(QPalette::Window, YigColorKit::background2);
    yigPalette.setColor(QPalette::Button, YigColorKit::focus1);
    yigPalette.setColor(QPalette::ButtonText, YigColorKit::background2);
    setStyle(yigStyle);
    setPalette(yigPalette);
    scrollView.setStyle(yigStyle);
    QPalette scrollPalette = scrollView.palette();
    scrollPalette.setColor(QPalette::Highlight, YigColorKit::background2);
    scrollPalette.setColor(QPalette::Button, YigColorKit::focus2);
    scrollPalette.setColor(QPalette::Window, YigColorKit::background2);
    scrollPalette.setColor(QPalette::Base, YigColorKit::background2);
    scrollPalette.setColor(QPalette::LinkVisited, YigColorKit::background2);
    scrollView.setPalette(scrollPalette);
    scrollView.setGeometry(geometry());
    scrollView.setFrameStyle(QFrame::NoFrame);

    scrollView.setWidgetResizable(true);
    scrollView.setWidget(&groupWidget);
    vLayout.addWidget(&scrollView);
    groupBox.setContentsMargins(0,0,20,20);
    groupBox.setSpacing(0);
    scrollView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    createSynthList();

    queuedSynth = "";
    delete yigStyle;
}

YigSynthMenu::~YigSynthMenu()
{
    while(!synthButtonList.isEmpty())
    {
        delete synthButtonList.takeFirst();
    }
    synthButtonList.clear();
}

QString YigSynthMenu::getQueuedSynth()
{
    return queuedSynth;
}

void YigSynthMenu::synthClick(QString synthName)
{
    queuedSynth = synthName;
}

void YigSynthMenu::clearQueuedSynth()
{
    queuedSynth = "";
}

void YigSynthMenu::createSynthList()
{
    QFont synthFont("monaco", 11);

    QStringList filters;
    filters << "*.scsyndef";
    QStringList synthNameList = synthDirectory.entryList(filters);
    synthNameList.removeAll("YigCable.scsyndef");
    synthNameList.removeAll("YigRecord.scsyndef");
    synthNameList.removeAll("YigSilence.scsyndef");
    synthNameList.removeAll("libsc++-server-record.scsyndef");

    // Create the synth buttons, connect their signals to the menu slots, and add the to the scrollView
    for(int i = 0; i < synthNameList.size(); i++)
    {
        QString synthDefName = synthNameList.at(i);
        synthDefName.remove(".scsyndef");
        YigSynthButton* button = new YigSynthButton(synthDefName, this);
        QObject::connect(button, SIGNAL(pressed(QString)), this, SLOT(synthClick(QString)));
        button->setFont(synthFont);
        button->setFocusPolicy(Qt::NoFocus);
        groupBox.addWidget(button);
        synthButtonList.append(button);
    }
}
